<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

aos_header( 'Horse races' );
?>
					<div class="blok">
<?php
printDiplomacyTab( 'Horse races' ); 
?>
					</div>
					<br />
					<div class="blok">
<?php
	if( isset( $_SESSION[ 'place_bet_status' ] ) ) {
		print( translate( "Status" ) . ": " . $_SESSION[ 'place_bet_status' ] . "<br /><br />\r\n" );
		unset( $_SESSION[ 'place_bet_status' ] );
	}
	$current_turn = getCurrentTurn();
	$query = "SELECT count(*) " .
			"FROM race_teams";
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	$teams_total = $row[ 'count(*)' ];
	if( $teams_total > 1 ) {
		$query = "SELECT * FROM races WHERE turn >= $current_turn ORDER BY turn DESC LIMIT 1";
		$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$row = mysql_fetch_array( $result, MYSQL_ASSOC );
		if( isset( $row[ 'race_id' ] ) ) {
			$prize_money = $row[ 'prizes' ];
			$next_turn = $row[ 'turn' ];
			$turn = $next_turn - $current_turn;
			$race_id = $row[ 'race_id' ];
			if( $turn == 1 ) {
				$next_race = "<a href=\"race.php?race_id=" . $race_id .  "\">" . translate( 'in 1 day' ) . "</a>";
			}
			else {
				$next_race = "<a href=\"race.php?race_id=" . $race_id .  "\">" . translate( 'in' ) . " $turn " . translate( 'days' ) . "</a>";
			}
		}
		else {
			$next_race = translate( 'Not scheduled yet.' );
		}
		print( "<strong>" . translate( 'International Horse Race League' ) . "</strong><br />\r\n" );
		print( "<a href=\"raceteam.php\">" . translate( 'Teams' ) . ": " . number_format( $teams_total ) . "</a><br />\r\n" );
		print( "" . translate( 'Next race' ) . ": " . $next_race . "<br />\r\n" );
		if( isset( $prize_money ) ) {
			print( translate( 'Total prize gold' ) . ": <strong class=\"gold\">" . number_format( $prize_money ) . "</strong><br />\r\n" );
			print( translate( '1st place reward' ) . ": <strong class=\"gold\">" . number_format( round( $prize_money * 0.6 ) ) . "</strong><br />\r\n" );
			print( translate( '2nd place reward' ) . ": <strong class=\"gold\">" . number_format( round( $prize_money * 0.3 ) ) . "</strong><br />\r\n" );
			print( translate( '3rd place reward' ) . ": <strong class=\"gold\">" . number_format( round( $prize_money * 0.1 ) ) . "</strong><br />\r\n" );
		}
		print( "<br />\r\n" );
		if( isset( $next_turn ) ) {
			$query = "SELECT * FROM races WHERE turn < $next_turn ORDER BY turn DESC LIMIT 1";
			$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			$row = mysql_fetch_array( $result, MYSQL_ASSOC );
			if( isset( $row[ 'race_id' ] ) ) {
				$prize_money = $row[ 'prizes' ];
				$date = $row[ 'date' ];
				$race_id = $row[ 'race_id' ];
				print( "<strong>" . translate( 'Last race' ) . "</strong><br />\r\n" );
				print( "" . translate( 'Date' ) . ": <a href=\"race.php?race_id=" . $race_id .  "\">" . $date . "</a><br />\r\n" );
				
				$query = "SELECT race_history.race_team_id, race_teams.name FROM race_history JOIN race_teams ON race_history.race_team_id = race_teams.race_team_id WHERE race_id = $race_id ORDER BY rank ASC LIMIT 0, 3";
				$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
				$num = mysql_numrows( $result );
				$i = 0;
				while( $num > $i ) {
					if( $i == 0 ) {
						$first = "<a href=\"raceteam.php?team_id=" . mysql_result( $result, $i, "race_team_id" ) . "\">" . mysql_result( $result, $i, "name" ) . "</a>";
					}
					else if( $i == 1 ) {
						$second = "<a href=\"raceteam.php?team_id=" . mysql_result( $result, $i, "race_team_id" ) . "\">" . mysql_result( $result, $i, "name" ) . "</a>";
					}
					else if( $i == 2 ) {
						$third = "<a href=\"raceteam.php?team_id=" . mysql_result( $result, $i, "race_team_id" ) . "\">" . mysql_result( $result, $i, "name" ) . "</a>";
					}
					$i ++;
				}
				
				print( translate( 'Prize gold' ) . ": <strong class=\"gold\">" . number_format( $prize_money ) . "</strong><br />\r\n" );
				print( translate( '1st' ) . ": $first (Prize: <strong class=\"gold\">" . number_format( round( $prize_money * 0.6 ) ) . "</strong>)<br />\r\n" );
				print( translate( '2nd' ) . ": $second (Prize: <strong class=\"gold\">" . number_format( round( $prize_money * 0.3 ) ) . "</strong>)<br />\r\n" );
				if( isset( $third ) ) {
					print( translate( '3rd' ) . ": $third (Prize: <strong class=\"gold\">" . number_format( round( $prize_money * 0.1 ) ) . "</strong>)<br /><br />\r\n" );
				}
			}
		}
		
		sponsorRaceTeamForm();
	}
	else {
		print( translate( 'There are not enough race horse teams signed up for the international league, yet...' ) . "<br /><br />\r\n" );
		sponsorRaceTeamForm();
	}
?>
				</div> <!-- close blok -->
			</div> <!-- close container -->
		</div> <!-- close root-container -->
	</body>
</html><?php
function sponsorRaceTeamForm() {
	if( isset( $_SESSION[ 'sponsor_team_status' ] ) ) {
		print( translate( 'Status' ) . ": " . $_SESSION[ 'sponsor_team_status' ] . "<br/><br/>\r\n" );
		unset( $_SESSION[ 'sponsor_team_status' ] );
	}

	$query = "SELECT faction_id FROM faction_member WHERE invited = 0 AND requested = 0 AND ruler_id = " . $_SESSION[ 'ruler_id' ];
	$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$row = mysql_fetch_array( $result, MYSQL_ASSOC );
	if( isset( $row[ 'faction_id' ] ) ) {
		$faction_id = $row[ 'faction_id' ];
		$query = "SELECT * FROM race_teams WHERE faction_id = " . $faction_id;
		$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
		$row = mysql_fetch_array( $result, MYSQL_ASSOC );
		if( isset( $row[ 'race_team_id' ] ) ) {
			$race_team_id = $row[ 'race_team_id' ];
			$race_name = $row[ 'name' ];
			$query = "SELECT count(*) FROM race_teams";
			$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			$row = mysql_fetch_array( $result, MYSQL_ASSOC );
			if( $row[ 'count(*)' ] < 2 ) {
				print( translate( 'We are the only ones with a' ) . " <a href=\"raceteam.php?team_id=" . $race_team_id . "\">" . translate( 'horse race team' ) . "</a> " . translate( 'at the moment' ) . "!<br />" . translate( 'Let\'s wait for some competition.' ) . "" );
			}
			else {
				print( translate( 'Our sponsored team' ) . ": <a href=\"raceteam.php?team_id=" . $race_team_id . "\">" . $race_name . "</a>" );
			}
		}
		else {
			$query = "SELECT * FROM faction WHERE leader = " . $_SESSION[ 'ruler_id' ];
			$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
			$row = mysql_fetch_array( $result, MYSQL_ASSOC );
			if( isset( $row[ 'faction_id' ] ) ) {
				// This is the leader and he can make a new race team
				if( $row[ 'gold' ] > 1000 ) {
				?>
					<form action="sponsor_team.php" method="post">
						Sponsor a horse race team with our faction for <strong class="gold">1,000</strong> gold: <input type="submit" value="Sponsor" /><br /><br />
					</form>
				<?php
				}
				else {
					print( translate( 'Our faction does not have enough gold, it costs <strong class="gold">1,000</strong> gold to sponsor a horse race team.' ) . "<br />\r\n" );
				}
				print( translate( 'Our current gold reserves are' ) . " <strong class=\"gold\">" . number_format( $row[ 'gold' ] ) . "</strong> " . translate( 'gold' ) . "." );
			}
			else {
				print( translate( 'Ask your faction leader to sponsor a horse race team!' ) );
			}
		}
	}
	else {
		print( translate( 'Only faction leaders can sponsor horse race teams.' ) );
	}
}
aos_footer();
?>


